/*
 * @Author: Jim <jimjiang0909@gmail.com>
 * @Create time:   2016-06-14 15:20:07
 * @Last Modified by:   Jim
 * @Last Modified time: 2016-08-29 11:36:48
 * @File Name: utils.js
 */

import React from 'react';
import {
    View,
    Text,
    PixelRatio,
    ActivityIndicator,
    InteractionManager,
} from 'react-native';

import Dimensions from 'Dimensions';

const Util = {

    ratio: PixelRatio.get(),

    pixel: 1 / PixelRatio.get(),

    size: {
        width: Dimensions.get('window').width,
        height: Dimensions.get('window').height
    },

    navJumpTo(navigator, route , menu) {
        const currentRouteStack = navigator.getCurrentRoutes();
        let histroyRoute;

        // if (Routes.length == 0) {
        //     Routes.push('Index');
        // }

        // let stack = currentRouteStack[0].stack;
        if (route.name !== Routes[Routes.length - 1]) {
            Routes.push(route.name);
        }

        currentRouteStack.some((item, index) => {
            if (route.name === item.name) {
                histroyRoute = currentRouteStack[index];
                return true;
            }
        });

        if (histroyRoute) {
            navigator.jumpTo(histroyRoute,menu);
        } else {
            navigator.push(route);
        }
    },

    navGoBack(navigator) {
        if (Routes.length < 1) {return;}
        
        const currentRouteStack = navigator.getCurrentRoutes();
        // const stack = currentRouteStack[0].stack;
        Routes.pop();
        const prevRouteName = Routes[Routes.length - 1];
        let prevRoutePosition;

        currentRouteStack.some((item, index) => {
            if (item.name === prevRouteName) {
                prevRoutePosition = index;
                return true;
            }
        });
        
        try {
            navigator.jumpTo(currentRouteStack[prevRoutePosition]);
        } catch (err) {
            navigator.pop();
        }
    },

    navClearTo(navigator,route){
        Routes = [route.name];        
        navigator.clearAndPush(route);
    },

    navResetTo(navigator,route) {
        Share.loginOut();
        Routes = [route.name];
        navigator.resetTo(route);
    },

    navReplaceTo(navigator, route) {
        const currentRouteStack = navigator.getCurrentRoutes();
        let histroyIndex;

        if (route.name !== Routes[Routes.length - 1]) {
            Routes.push(route.name);
        }

        currentRouteStack.some((item, index) => {
            if (route.name === item.name) {
                histroyIndex = index;
                return true;
            }
        });
        if (histroyIndex) {
            navigator.replaceAtIndex(route, histroyIndex, this.navJumpTo(navigator, route));
        } else {
            this.navJumpTo(navigator, route);
        }
    },

    dateFormat : function (data,fmt) {
            var o = {
                "M+": data.getMonth() + 1, //月份
                "d+": data.getDate(), //日
                "h+": data.getHours(), //小时
                "m+": data.getMinutes(), //分
                "s+": data.getSeconds(), //秒
                "q+": Math.floor((data.getMonth() + 3) / 3), //季度
                "S": data.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (data.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
    },

    lastNumMonth : function(num,date) {
         var h_month,h_day,h_year,c_month,c_dayc_year,max_h_day;
             if (date) {
                 c_month = date.getMonth();
             } else {
                 date = new Date();
                 c_month = date.getMonth()+1;
             }
             c_day = date.getDate();
             c_year = date.getFullYear();

        if ((c_month - num) > 0) {
            h_month = c_month - num;
            h_year = c_year;
        } else {
            h_month = (c_month+Math.ceil(num/12)*12)-num;
            h_year = c_year-Math.ceil(num/12);
        }

        max_h_day = new Date(h_year,h_month,0).getDate();

        if (c_day < max_h_day) {
            h_day = c_day;
        } else {
            h_day = max_h_day;
        }
        return  Util.FormatDate(h_year,h_month,h_day);
    },

    FormatDate : function(Year,Month,Day) {
           var CurrentDate = Year + "-";
           if (Month >= 10 ) {
              CurrentDate += Month + "-";
           } else {
              CurrentDate += "0" + Month + "-";
           }
           if (Day >= 10 ) {
              CurrentDate += Day ;
           } else {
              CurrentDate += "0" + Day ;
           }
           return CurrentDate;
    },

    age : function(year) {
        var y = new Date().getFullYear();
        return y - year;
    },

    getMultipleData : function(arr) {
        if( !_.isArray(arr) ) {
            return arr || 0;
        }
        var value = 0;
        for (var i in arr) {
             if(arr[i]) {
                value += parseInt(arr[i]);
             }
        }
        if (value == 0) {
            value = '';
        }
        return value;
    },

    isEmail(email) {
        var bool = true,
            filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (filter.test(email)) {
            bool = false;
        }
        return bool;
    },

    isLength(str,min,max) {
        var bool = false,len = str.length;
        if (len < min || len >max) {
              bool = true;
        }
        return bool;
    },

    loading(topHeight = 95) {
        return (
            <View style={{ backgroundColor: "#fff", height: this.size.height - topHeight, width: this.size.width, justifyContent: 'center', alignItems: 'center' }}>
                    <ActivityIndicator
                        animating={true}
                        size={'large'}
                     />

            </View>
        );
    },

    loadingMore() {
       return (
            <View style={{ backgroundColor: "#fff", height: 50, width: this.size.width, justifyContent: 'center', alignItems: 'center' }}>
                    <ActivityIndicator
                        animating={true}
                        size={'small'}
                     />

            </View>
        ); 
    },

    noMoreData() {
       return (
            <View style={{ backgroundColor: "#fff", height: 50, width: this.size.width, justifyContent: 'center', alignItems: 'center' }}>
                    <Text> No More Data</Text>

            </View>
        ); 
    },

    noData() {
       return (
            <View style={{ backgroundColor: "#fff", height: this.size.height, width: this.size.width, justifyContent: 'center', alignItems: 'center' }}>
                    <Text> No Data</Text>
            </View>
        ); 
    },

    replaceGender(str) {
        if(str != null) {
           str = str.replace(/man/g,"men");
           str = str.replace(/Man/g,"Men");
           str = str.replace(/Couple/g,"Couples");
           str = str.replace(/Cub \/ Men/g,"Cubs / Men");
           str = str.replace(/Cougar/g,"Cougars");
        }
        return str;
    },

};

export default Util;